package pc

import (
	. "chess_platform/internal/common"
	"fmt"
	"github.com/astaxie/beego/orm"
)

//添加跑马灯
func NewHorse(data map[string]interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessSysData)
	values, sql := InsertSql(TableHorse, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

//更新跑马灯
func UpdateHorse(data map[string]interface{}, id int64) (int64, error) {
	condition := fmt.Sprintf(`id = %v`, id)
	values, sql := UpdateSql(TableHorse, data, condition)
	o := NewDBOrm(DBChessSysData)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//删除跑马灯
func DeleteHorse(id int64, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChessSysData)
	condition := fmt.Sprintf("id = %v", id)
	sql := DeleteSql(TableHorse, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//跑马灯列表
func ListHorse(p ListParams) ([]*Horse, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		ORDER BY
			id DESC
		LIMIT ?,?
		`, TableHorse)
	o := NewDBOrm(DBChessSysData)
	var gl []*Horse
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&gl)
	if CheckError(err) {
		return nil, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `, TableHorse)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	return gl, count, err
}
